import { defineStore } from 'pinia'
import { ref } from 'vue'
import router from '../router'

// 定义用户角色类型
export type UserRole = 'admin' | 'maintainer' | 'user' | 'visitor' | ''

// 定义用户信息接口
export interface UserInfo {
  username: string
  role: UserRole
  title?: string
  tasks?: number
}

// 硬编码的用户账号
const USERS = [
  { username: 'admin', password: 'admin123', role: 'admin' as UserRole },
  { username: 'maintainer', password: 'maint123', role: 'maintainer' as UserRole, title: '高级养护工程师', tasks: 8 },
  { username: '张工', password: 'maint123', role: 'maintainer' as UserRole, title: '养护工程师', tasks: 5 },
  { username: '李工', password: 'maint123', role: 'maintainer' as UserRole, title: '养护技术员', tasks: 3 },
  { username: '王工', password: 'maint123', role: 'maintainer' as UserRole, title: '养护工程师', tasks: 6 },
  { username: 'user1', password: 'user123', role: 'user' as UserRole }
]

export const useUserStore = defineStore('roadUser', () => {
  // 用户信息
  const userInfo = ref<UserInfo>({
    username: '',
    role: ''
  })

  // 登录状态
  const isLoggedIn = ref(false)

  // 访客模式
  const isVisitor = ref(false)

  // 登录方法
  function login(username: string, password: string): boolean {
    const user = USERS.find(u => u.username === username && u.password === password)

    if (user) {
      userInfo.value = {
        username: user.username,
        role: user.role
      }
      isLoggedIn.value = true
      isVisitor.value = false
      return true
    }

    return false
  }

  // 访客登录方法
  function loginAsVisitor(redirectPath?: string) {
    userInfo.value = {
      username: '访客',
      role: 'visitor'
    }
    isLoggedIn.value = false
    isVisitor.value = true

    // 如果提供了重定向路径，则跳转到该路径，否则跳转到欢迎页
    if (redirectPath) {
      router.push(redirectPath)
    } else {
      router.push('/welcome')
    }
  }

  // 注册方法
  function register(username: string, password: string, role: UserRole): boolean {
    // 检查用户名是否已存在
    const existingUser = USERS.find(u => u.username === username)
    if (existingUser) {
      return false
    }

    // 添加新用户
    USERS.push({ username, password, role })

    // 自动登录
    userInfo.value = {
      username,
      role
    }
    isLoggedIn.value = true
    isVisitor.value = false

    return true
  }

  // 登出方法
  function logout() {
    userInfo.value = {
      username: '',
      role: ''
    }
    isLoggedIn.value = false
    isVisitor.value = false
    router.push('/login')
  }

  // 获取所有养护人员
  function getMaintainers() {
    return USERS.filter(user => user.role === 'maintainer').map(user => ({
      id: user.username,
      name: user.username,
      role: user.title || '养护人员',
      tasks: user.tasks || 0,
      active: true
    }))
  }

  // 获取所有用户
  function getAllUsers() {
    return USERS.map(user => ({
      username: user.username,
      role: user.role,
      title: user.title,
      tasks: user.tasks
    }))
  }

  return {
    userInfo,
    isLoggedIn,
    isVisitor,
    login,
    loginAsVisitor,
    register,
    logout,
    getMaintainers,
    getAllUsers
  }
}, {
  persist: true // 持久化存储用户状态
})
